﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;
using PromotionEngine.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace PromotionEngine
{
    public partial class HierarchyDetailTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetData(string hierarchyID, string hierarchyName)
        {

            Promotion pmt = new Promotion();

            DataSet dsData = pmt.GetHierarchyDetailByHierarchyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), hierarchyID);
            if (dsData != null && dsData.Tables.Count > 0)
            {
                List<HierarchyDetail> locations = new List<HierarchyDetail>();
                HierarchyDetail dataRoot = new HierarchyDetail();
                dataRoot.HierarchyID = hierarchyID;
                dataRoot.HierarchyFatherID = "-1";
                dataRoot.HierarchyDetailName = hierarchyName;
                dataRoot.HierarchyDetailID = "-1";
                locations.Add(dataRoot);
                foreach (DataRow r in dsData.Tables[0].Rows)
                {
                    HierarchyDetail data = new HierarchyDetail();
                    data.HierarchyID = r["HierarchyID"].ToString();
                    data.HierarchyFatherID = r["HierarchyFatherID"].ToString();
                    data.HierarchyDetailID = r["HierarchyDetailID"].ToString();
                    data.HierarchyDetailName = r["HierarchyDetailName"].ToString();
                    locations.Add(data);
                }

                Dictionary<string, HierarchyDetail> dict = locations.ToDictionary(loc => loc.HierarchyDetailID);

                foreach (HierarchyDetail loc in dict.Values)
                {
                    if (loc.HierarchyFatherID != loc.HierarchyDetailID && loc.HierarchyFatherID != "0")
                    {
                        HierarchyDetail parent = dict[loc.HierarchyFatherID];
                        parent.items.Add(loc);
                    }
                }

                HierarchyDetail root = dict.Values.First(loc => loc.HierarchyFatherID == loc.HierarchyDetailID);

                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Formatting = Formatting.Indented
                };
                string json = JsonConvert.SerializeObject(root, settings);


                return json;
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateHierarchyDetail(string hierarchyDetailID,string hierarchyDetailName)
        {
            if (HttpContext.Current.Session["LoginUser"] != null)
            {
                hierarchyDetailName= hierarchyDetailName.Replace("\"", "").Trim();
                Promotion pmt = new Promotion();
                string errMsg = pmt.UpdateHierarchyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), hierarchyDetailID,hierarchyDetailName);
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteHierarchyDetail(string hierarchyDetailID)
        {
            if (HttpContext.Current.Session["LoginUser"] != null)
            {
                Promotion pmt = new Promotion();
                string errMsg = pmt.DeleteHierarchyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), hierarchyDetailID);
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void CreateHierarchyDetail(string oHierarchyDetail)
        {
            Model.HierarchyDetail hierarchy = JsonConvert.DeserializeObject<Model.HierarchyDetail>(oHierarchyDetail);
            if (HttpContext.Current.Session["LoginUser"] != null)
            {
                DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                Promotion pmt = new Promotion();
                string headerJson = JsonConvert.SerializeObject(hierarchy);
                string errMsg = pmt.CreateHierarchyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), hierarchy.HierarchyID, hierarchy.HierarchyDetailName, hierarchy.HierarchyFatherID);
            }
        }
    }
}